package com.spontaneappnew;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphUser;

public class FBUser {
	
	private static FBUser user;
	private String id;
	private String userName;
	
	private FBUser(String id, String username){
		this.id = id;
		this.userName=username;
	}
	
	/*
	 * Returns the singleton of the Facebook user, initializes on the first time.
	 * The call to the Facebook Graph is synchrinized but always called from an Async task.
	 */
	public static FBUser getInstance(){
		if (user == null){
			// Check for an open FB session
	        final Session session = Session.getActiveSession();
	        if (session != null && session.isOpened()) {
	        	// Make an API call to get user data and define a 
	        	// new callback to handle the response.
	        	Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
		       		@Override
		       		public void onCompleted(GraphUser userFB, Response response) {
		       			if (session == Session.getActiveSession()) {
		       				if (userFB!= null) {
		       					user = new FBUser(userFB.getId(), userFB.getName());
		       					Places.getInstance().startGettingFromServer(userFB.getId());
		       				}
		      			}
	        			if (response.getError() != null) {
	        			}
	        		}
	        	});
	        	request.executeAndWait();
	        }
	      }  
		return user;
	}
	
	public String getID(){
		return this.id;
	}
	
	public String getName(){
		return this.userName;
	}
	
}